<?php

class admin{

	function __construct(){
	
	}
	
	public function show_pages(){
		$query = mysql_query("SELECT * FROM pages  ORDER BY title") or die (mysql_error());
		if ($query){
			$html='<table class="pages">';
			while ($result = mysql_fetch_array($query)){
				$html.='<tr><td><a class="show_page" href="profile.php?action=update_page&id_page='.$result['id'].'">'.$result['title'].'</a>';
				$html.='<input type="button" title="Удалить" class="delete_page"/>';
				if ($result['in_menu'])
					$html.='<input type="button" title="Добавить страницу в меню" class="in_menu_button"/></td></tr>';
				else
					$html.='<input type="button" title="Добавить страницу в меню" class="no_in_menu_button"/></td></tr>';
			}
			$html.='</table>';
			return $html;
		}
		return false;
	}
	
	public function find_pages($title){
	$title = '%'.$title.'%';
		$query = mysql_query("SELECT * FROM pages WHERE title LIKE '".$title."' ORDER BY title") or die (mysql_error());
		if ($query){
			$html='<table class="pages">';
			while ($result = mysql_fetch_array($query)){
				$html.='<tr><td><a class="show_page" href="profile.php?action=update_page&id_page='.$result['id'].'">'.$result['title'].'</a>';
				$html.='<input type="button" title="Удалить" class="delete_page"/>';
				if ($result['in_menu'])
					$html.='<input type="button" title="Добавить страницу в меню" class="in_menu_button"/></td></tr>';
				else
					$html.='<input type="button" title="Добавить страницу в меню" class="no_in_menu_button"/></td></tr>';
			}
			$html.='</table>';
			return $html;
		}
		return false;
	}
	
	public function show_users(){
		$query = mysql_query("SELECT * FROM user WHERE privilege<3 ORDER BY login") or die (mysql_error());
		$html='<table class="users">';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a>';
			$html.='<input type="button" class="delete_user"/>';
			$html.='<a id="'.$result['id'].'" class="message_admin_user"></a></td></tr>';
		}
		$html.='</table>';
		return $html;
	}
	
	public function find_users($find){
		$find = $find.'%';
		$query = mysql_query("SELECT * FROM user WHERE privilege<3 and (id LIKE '".$find."' or login LIKE '".$find."' or name LIKE '".$find."' or surname LIKE '".$find."') ORDER BY login") or die (mysql_error());
		$html='<table class="users">';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a>';
			$html.='<input type="button" class="delete_user"/>';
			$html.='<a id="'.$result['id'].'" class="message_admin_user"></a></td></tr>';
		}
		$html.='</table>';
		return $html;
	}
	
	public function parent_for_user(){
		$query = mysql_query("SELECT * FROM user WHERE privilege<3 ORDER BY login") or die (mysql_error());
		$html='<TABLE>';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_page" href="profile.php?action=parent_user&id_user='.$result['id'].'">'.$result['login'].'</a>';
			$html.='<input type="button" class="delete_user"/></td></tr>';
		}
		$html.='</TABLE>';
		return $html;
	}
	
	public function select_pages($id){
		$query = mysql_query("SELECT * FROM pages WHERE id='".$id."'") or die (mysql_error());
		$result = mysql_fetch_array($query);
		return $result;
	}
	
	public function select_idpage($title){
		$query = mysql_query("SELECT id FROM pages WHERE title = '".$title."'") or die (mysql_error());
		$result = mysql_fetch_array($query);
		return $result['id'];
	}
	
	public function delete_page($id){
		if (mysql_query("DELETE FROM pages WHERE id = '".(int)$id."'"))			
			return true;
		return false;
	}
	
	public function update_page($data){
		if(mysql_query("UPDATE pages SET title = '".$data['title']."',content = '".$data['content']."',in_menu = '".$data['in_menu']."',change_date = '".time()."', seo_title = '".$data['seo_title']."', seo_description = '".$data['seo_description']."', seo_keywords = '".$data['seo_keywords']."' WHERE id ='".$data['id']."'"))
			return true;
		return false;
	}
	
	public function upd_in_menu($id,$val){
		if(mysql_query("UPDATE pages SET `in_menu` = '".$val."' WHERE id ='".$id."';"))
			return true;
		return false;
	}
	
	public function add_page($data){
		$query = mysql_query("SELECT title FROM pages WHERE title = '".$data['title']."'") or die (mysql_error());
		$result = mysql_num_rows($query);
		if ($result == 0){
			if(mysql_query("INSERT INTO pages (title ,content ,in_menu ,author ,creation_date,change_date, seo_title, seo_description, seo_keywords) VALUES ('".$data['title']."', '".$data['content']."', '".$data['in_menu']."', '".$data['author']."', '".$data['creation_date']."','".time()."', '".$data['seo_title']."', '".$data['seo_description']."', '".$data['seo_keywords']."')"))
				return true;
			return false;
		}
		else
			return false;
	}
	
	public function change_privilege($id,$privilege){
		if(mysql_query("UPDATE user SET privilege = '".$privilege."' WHERE id ='".$id."'")) 
			return true;
		return false;
	}
	
	public function delete_user($id){
		if (mysql_query("DELETE FROM user WHERE id = '".(int)$id."'"))			
			return true;
		return false;
	}
	
	public function select_user($id){
		$query = mysql_query("SELECT * FROM user WHERE id='".$id."'") or die (mysql_error());
		$result = mysql_fetch_array($query);
		return $result;
	}
	
	public function update_user($data){
		if ($query = mysql_query("UPDATE user SET name = '".$data['name']."',surname = '".$data['surname']."',phone = '".$data['phone']."',country = '".$data['country']."',city = '".$data['city']."',question = '".$data['question']."',response = '".$data['response']."', privilege = '".$data['privilege']."' WHERE id = '".$data['id']."'"))
			return true;
		return false;
	}

	public function TemplateAddUser($data){
		$salt = $this->generate_salt();
		$date = time();
		$password = md5(md5($data['password']).md5($salt));
		$result = "INSERT INTO temp_user (
			login,password,salt,name,surname,phone,email,country,city,question,response,registered,confirm,privilege)
			VALUES ('".$data['login']."','".$password."','".$salt."','".$data['name']."','".$data['surname']."','".$data['phone']."','".$data['email']."','".$data['country']."', '".$data['city']."','".$data['question']."','".$data['response']."','".$date."','".$data['confirm_reg']."','".$data['privilege']."')";
		$query=mysql_query($result) or die(mysql_error());
		if ($query) return true;				
		return false;
	}
	
	public function in_menu(){
		$query = mysql_query("SELECT id,title from pages WHERE in_menu = 1") or die (mysql_error);
		$menu = array();
		while ($result = mysql_fetch_array($query)){
			$menu[] = array('id'=>(int)$result['id'], 'title' => $result['title']);
		}
		return $menu;
	}
		
		
	public function mas_1($id){
		$query = mysql_query("SELECT * FROM gamers WHERE userParent='".$id."'") or die (mysql_error);
		$query_ = mysql_num_rows($query);
		if ($query_){
		$res[0] = -1;
			$i = 0;
			while ($result = mysql_fetch_array($query)){
				$res[$i]= $result['UserChild'];
				$i++;
			}				
			return $res;
		}
		return false;
	}
	
	public function mas_2($result){
		/*$result = $this->mas_1($id);*/
		$n = count($result);
		$j = 0;
		if ($n != 0){
			for ($i = 0; $i < $n; $i++){
				
				$result1 = $this->mas_1($result[$i]);
				$count = count($result1);
				for ($k = 0; $k < $count; $k++){						
					$res[$j] = $result1[$k];
					$j++;
				}
			}			
			return $res;
		}
		return false;
	}
	
	/*public function mas_3($id){
		$result = $this->mas_2($id);
		$n = count($id);
		$j = 0;
		if ($n != 0){
			for ($i = 0; $i < $n; $i++){
				
				$result1 = $this->mas_1($result[$i]);
				$count = count($result1);
				for ($k = 0; $k < $count; $k++){
					
					echo $res[$j] = $result1[$k];
					$j++;
				}
			}
		
			return $res;
		}
	}*/
	
	
}
$admin_obj = new admin();
?>